package com.gitee.shoes.crawler.core.shihuo;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.Method;
import com.gitee.shoes.crawler.bean.*;
import com.gitee.shoes.crawler.core.AbstractApiRequest;
import com.gitee.shoes.crawler.core.ApiRequestUtils;
import com.gitee.shoes.crawler.exception.ApiRequestException;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LYH
 */
@Slf4j
public class ShihuoApiRequest extends AbstractApiRequest {

    private interface Path {
        String host = "www.shihuo.cn";
        String base = "https://" + host;
        String detail = base + "/app_swoole_zone/getAttr";
        String list = base + "/app_swoole_goods/list";
        String buy_now_info = detail;
    }


    // page 从1开始
    // 默认返回20条
    @Override
    public ApiProductList productList(String keyword, Integer page, Integer limit, String nextKey, OrderMode orderMode) {
        String sort = "hot";
        if (OrderMode.news.equals(orderMode)) {
            sort = "new";
        } else if (OrderMode.priceDesc.equals(orderMode)) {
            sort = "price_d";

        } else if (OrderMode.priceAse.equals(orderMode)) {
            sort = "price_a";
        }
        String params = StrUtil.format("page={}&range=篮球鞋,跑鞋,休闲鞋,足球鞋,训练鞋,滑板鞋,网球鞋,高尔夫鞋,户外鞋,羽毛球鞋,拖鞋,凉鞋,皮鞋,靴子" +
                        "&search_rang=&c=&sort={}&brand=&price_from=&price_to=&keywords={}" +
                        "&tag_type=&size=&color=&show_type=&child_brand=&v=5.8.0&token={}", page, sort, keyword,
                IdUtil.simpleUUID());
        ShihuoResp<ItemData> shihuoResp = request(Method.POST, Path.list, params, new TypeReference<ShihuoResp<ItemData>>() {
        });
        ItemData data = shihuoResp.getData();
        List<ItemData_Lists> lists = data.getLists();
        List<ApiProduct> productItems = lists.stream().map(e -> new ApiProduct().setMainId(e.getGoodsId()).setStyleId(e.getStyleId())
                .setLogoUrl(e.getImg()).setTitle(e.getName())
                .setMinSalePrice(e.getPriceRange()).setSoldInfo(e.getSalenumFormat())
                .setArticle(null)).collect(Collectors.toList());
        ApiProductList productItemList = new ApiProductList().setItemList(productItems);
        return productItemList;
    }

    @Override
    public ApiProduct productDetail(String mainId, String styleId) {
        ApiProduct productDetail = new ApiProduct()
                .setMainId(mainId);

        return productDetail;
    }

    @Override
    public ApiProductBuyNowList productBuyNowList(String mainId, String styleId, List<ApiProductSize> productSizeList) {
        String url = StrUtil.format("{}?platform=wechat&v=5.4.0&id={}&news_id=&page=1&page_size=10&size=" +
                        "&sourceType=&special_id=&style_id={}&supplier_id=&sort=&token={}", Path.buy_now_info, mainId, styleId,
                IdUtil.simpleUUID());
        ShihuoResp<BuyNowData> shihuoResp = request(Method.GET, url, null, new TypeReference<ShihuoResp<BuyNowData>>() {
        });
        BuyNowData data = shihuoResp.getData();
        List<BuyNowData_Size> sizes = data.getSize();
        List<ApiProductBuyNow> productBuyNows = new ArrayList<>();
        sizes.forEach(e -> {
            String size = ApiRequestUtils.handlerSize(e.getName());
            String minPrice = e.getPrice();
            if (minPrice != null && !"0".equals(minPrice)) {
                productBuyNows.add(new ApiProductBuyNow().setSize(size).setMinSalePrice(minPrice).setTradeTypeName("普通"));
            }
        });
        return new ApiProductBuyNowList().setBuyNowList(productBuyNows);
    }

    @Override
    public ApiProductLastSoldList productLastSoldList(String mainId, String styleId, Integer limit, String lastId) {
        throw new ApiRequestException("不支持的接口");
    }



    @Override
    protected void fillRequest(HttpRequest request) {

    }

}